$(function () {

    if (!window.base.getLocalStorage('token')) {
        window.location.href = 'login.html';
    }

    var pageIndex = 1,
        moreDataFlag = true;
    getExpress(pageIndex);

    var ExpressStatusEnum = {
        STORED: 1,        // 已入库
        RESERVED: 2,      // 已预约
        PICKING: 3,       // 配货中
        OUTBOUND: 4,      // 已出库
        DELIVERED: 5,     // 已送达
        RECEIVED: 6       // 已签收
    };

    /*
    * 获取数据 分页
    * params:
    * pageIndex - {int} 分页下表  1开始
    */

    function getExpress(pageIndex) {
        var params = {
            url: 'explists',
            data: { page: pageIndex, pagesize: 20 },
            tokenFlag: true,
            sCallback: function (res) {
                var str = getExpressHtmlStr(res.result);
                $('#order-table').append(str);
            }
        };
        window.base.getData(params);
    }

    /*拼接html字符串*/
    function getExpressHtmlStr(res) {
        var data = res.data;
        if (data) {
            var len = data.length,
                str = '', item;
            if (len > 0) {
                for (var i = 0; i < len; i++) {
                    item = data[i];
                    str += '<tr>' +
                        '<td>' + item.id + '</td>' +
                        '<td>' + getExpName(item.company_code) + '</td>' +
                        '<td>' + item.tracking_number + '</td>' +
                        '<td>' + item.pickup_code + '</td>' +
                        '<td>' + getExpStatus(item.status) + '</td>' +
                        '<td>' + item.mobile + '</td>' +
                        '<td>' + item.address + '</td>' +
                        '<td>' + item.start_time + '</td>' +
                        '<td>' + item.end_time + '</td>' +
                        '<td data-id="' + item.id + '">' + getBtns(item.status) + '</td>' +
                        '</tr>';
                }
            }
            else {
                ctrlLoadMoreBtn();
                moreDataFlag = false;
            }
            return str;
        }
        return '';
    }

    function getTrackNum(tracking_number){
        if(is_null(tracking_number))tracking_number=""
        return tracking_number;
    }

    /*根据订单状态获得标志*/
    function getExpName(company_code) {
        var arr = [{
            cName: 'sf',
            txt: '顺丰'
        }, {
            cName: 'jd',
            txt: '京东'
        }, {
            cName: 'zto',
            txt: '中通'
        }, {
            cName: 'sto',
            txt: '申通'
        }, {
            cName: 'yto',
            txt: '圆通'
        }, {
            cName: 'yd',
            txt: '韵达'
        }, {
            cName: 'jt',
            txt: '极兔'
        }, {
            cName: 'ems',
            txt: '邮政'
        }];
        return '<span class="express-company-txt ' + arr[company_code - 1].cName + '">' + arr[company_code - 1].txt + '</span>';
    }

    /*根据订单状态获得标志*/
    function getExpStatus(status) {
        var arr = [{
            cName: 'store',
            txt: '已入库'
        }, {
            cName: 'reservation',
            txt: '已预约'
        }, {
            cName: 'picking',
            txt: '配货中'
        }, {
            cName: 'outbound',
            txt: '已出库'
        }, {
            cName: 'deliver',
            txt: '已送达'
        }, {
            cName: 'signin',
            txt: '已签收'
        }];
        return '<span class="express-status-txt ' + arr[status - 1].cName + '">' + arr[status - 1].txt + '</span>';
    }

    /*根据订单状态获得 操作按钮*/
    function getBtns(status) {
        var arr = [{
            cName: 'done',
            txt: '配货'
        }, {
            cName: 'outbound',
            txt: '出库'
        }];
        if (status == 2) {
            var index = 0;
            return '<span class="order-btn ' + arr[index].cName + '">' + arr[index].txt + '</span>';
        }  else if (status == 3) {
            index = 1;
            return '<span class="order-btn ' + arr[index].cName + '">' + arr[index].txt + '</span>';

        }else {
            return '';
        }
    }

    /*控制加载更多按钮的显示*/
    function ctrlLoadMoreBtn() {
        if (moreDataFlag) {
            $('.load-more').hide().next().show();
        }
    }

    /*加载更多*/
    $(document).on('click', '.load-more', function () {
        if (moreDataFlag) {
            pageIndex++;
            getOrders(pageIndex);
        }
    });
    /*配货*/
    $(document).on('click', '.order-btn.done', function () {
        var $this = $(this),
            $td = $this.closest('td'),
            $tr = $this.closest('tr'),
            id = $td.attr('data-id'),
            $tips = $('.global-tips'),
            $p = $tips.find('p');
        var params = {
            url: 'express',
            type: 'put',
            data: { id: id ,status:ExpressStatusEnum.PICKING},
            tokenFlag: true,
            sCallback: function (res) {
                if (res.code.toString().indexOf('2') == 0) {
                    $tr.find('.express-status-txt')
                        .removeClass('reservation').addClass('picking')
                    $tr.find('.order-btn')
                        .removeClass('done').addClass('outbound')
                        .text('出库');
                    $p.text('操作成功');
                } else {
                    $p.text('操作失败');
                }
                $tips.show().delay(1500).hide(0);
            },
            eCallback: function () {
                $p.text('操作失败');
                $tips.show().delay(1500).hide(0);
            }
        };
        window.base.getData(params);
    });

    /*退出*/
    $(document).on('click', '#login-out', function () {
        window.base.deleteLocalStorage('token');
        window.location.href = 'login.html';
    });
});